package com.csht.service.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csht.mapper.CshtGoodsMapper;
import com.csht.model.CshtGoods;
import com.csht.page.PagerModel;
import com.csht.service.CshtGoodsService;
import com.csht.vo.goods.GoodsDetails;
import com.csht.vo.index.shangpinzhangqu.CshtGoodsVo_2;
import com.csht.vo.index.tenGoods.CshtGoodsVo;
import com.csht.vo.my.CshtGoodsVo_3;
import com.csht.vo.my.GoodsAndUserVo;

@Service
public class CshtGoodsServiceImpl implements CshtGoodsService {
	
	@Autowired
	private CshtGoodsMapper cshtGoodsMapper;

	/**
	 * 根据每日十件的商品id查询商品
	 */
	@Override
	public CshtGoodsVo getGoodsByGoodsId(Integer goodsId) throws Exception {
		CshtGoodsVo cshtGoodsVo = cshtGoodsMapper.getGoodsByGoodsId(goodsId);
		return cshtGoodsVo;
	}

	/**
	 * 随机查询十条商品id
	 */
	@Override
	public List<Integer> suiJiGetTenGoodsId() throws Exception {
		List<Integer> goodsIdList = cshtGoodsMapper.suiJiGetTenGoodsId();
		return goodsIdList;
	}

	/**
	 * 根据二级菜单id查找商品
	 */
	@Override
	public List<CshtGoodsVo_2> getGoodsByTwoMenuId(Integer twoMenuId) throws Exception {
		List<CshtGoodsVo_2> cshtGoodsVo_2List = cshtGoodsMapper.getGoodsByTwoMenuId(twoMenuId);
		return cshtGoodsVo_2List;
	}

	/**
	 * 有用户登录时，随机查询十条商品记录封装到每日十件区
	 */
	@Override
	public List<CshtGoodsVo> getTenGoodsToLoginUser(String goodsTradeSchool) throws Exception {
		List<CshtGoodsVo> cshtGoodsVoList = cshtGoodsMapper.getTenGoodsToLoginUser(goodsTradeSchool);
		return cshtGoodsVoList;
	}

	/**
	 * 根据用户id查找该用户正在出售商品的数量
	 */
	@Override
	public Integer getGoodsNumberByLoginUser(Integer userId) throws Exception {
		Integer count = cshtGoodsMapper.getGoodsNumberByLoginUser(userId);
		return count;
	}

	/**
	 * 根据定位地址变化查找商品（每日十件区）
	 */
	@Override
	public List<CshtGoodsVo> getGoodsByAddress(String address) throws Exception {
		List<CshtGoodsVo> cshtGoodsVo = cshtGoodsMapper.getGoodsByAddress(address);
		return cshtGoodsVo;
	}

	/**
	 * 定位改变时，根据二级菜单id和用户地址查找商品
	 */
	@Override
	public List<CshtGoodsVo_2> getGoodsByTwoMenuIdAndposition(Map<String, String> tIdAndPName) throws Exception {
		List<CshtGoodsVo_2> cshtGoodsVo_2List = cshtGoodsMapper.getGoodsByTwoMenuIdAndposition(tIdAndPName);
		return cshtGoodsVo_2List;
	}

	/**
	 * 根据商品id查找商品
	 */
	@Override
	public CshtGoodsVo_3 getGoodsByGoodsId_2(Integer goodsId) throws Exception {
		CshtGoodsVo_3 cshtGoodsVo_3 = cshtGoodsMapper.getGoodsByGoodsId_2(goodsId);
		return cshtGoodsVo_3;
	}

	/**
	 * 根据用户id查找商品出售中的商品
	 */
	@Override
	public PagerModel<CshtGoodsVo_3> getSellGoodsByUserId(Map<String, Integer> res) throws Exception {
		PagerModel<CshtGoodsVo_3> pagerModel = new PagerModel<CshtGoodsVo_3>(); //创建分页封装类
		List<CshtGoodsVo_3> cshtGoodsVo_3List = cshtGoodsMapper.getSellGoodsByUserId(res); //查询4条正在出售的商品
		Integer totalSize = cshtGoodsMapper.getGoodsNumberByLoginUser(res.get("userId")); //查询用户正在出售中商品的数量
		Integer messagePageSize = res.get("messagePageSize"); //获取每页的显示的记录数量
		Integer offset = res.get("offset"); //偏移量
		pagerModel.setDates(cshtGoodsVo_3List);
		pagerModel.setMessagePageSize(messagePageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据商品id查询商品(商品详细页按商品id查询商品)
	 */
	@Override
	public GoodsDetails getGoodsDetailData(Integer goodsId) throws Exception {
		GoodsDetails goodsDetails = cshtGoodsMapper.getGoodsDetailData(goodsId);
		return goodsDetails;
	}

	/**
	 * 根据用户id随机抽取6条商品，除本商品外(商品详细页按商品id查询商品)
	 */
	@Override
	public List<CshtGoods> getGoodsDetailToSixGoods(Map<String, Integer> res) throws Exception {
		List<CshtGoods> cshtGoodsList = cshtGoodsMapper.getGoodsDetailToSixGoods(res);
		return cshtGoodsList;
	}

	/**
	 * 添加商品并返回主键id
	 */
	@Override
	public void insertGoods(CshtGoods cshtGoods) throws Exception {
		cshtGoodsMapper.insertGoods(cshtGoods);
	}

	/**
	 * 更新商品封面图
	 */
	@Override
	public void updateGoodsImapath(CshtGoods cshtGoods) throws Exception {
		cshtGoodsMapper.updateGoodsImapath(cshtGoods);
	}

	/**
	 * 根据商品id查找商品
	 */
	@Override
	public CshtGoods selectGoodsById(Integer goodsId) throws Exception {
		CshtGoods cshtGoods = cshtGoodsMapper.selectGoodsById(goodsId);
		return cshtGoods;
	}

	/**
	 * 根据商品id更新goods_state为"已下架"
	 */
	@Override
	public void updateGoodsStateById(Integer goodsId) throws Exception {
		cshtGoodsMapper.updateGoodsStateById(goodsId);
	}

	/**
	 * 根据商品id更新goods_state为"出售中"
	 */
	@Override
	public void updateGoodsStateSellById(Integer goodsId) throws Exception {
		cshtGoodsMapper.updateGoodsStateSellById(goodsId);
	}

	/**
	 * 根据商品id更新goods_state为"已出售"
	 */
	@Override
	public void updateGoodsStateSuccessById(Integer goodsId) throws Exception {
		cshtGoodsMapper.updateGoodsStateSuccessById(goodsId);
	}

	/**
	 * 根据一级菜单查找出售中的商品
	 */
	@Override
	public PagerModel<GoodsAndUserVo> selectGoodsByOneMenuId(Map<String, Object> res) throws Exception {
		List<GoodsAndUserVo> goodsAndUserVoList = cshtGoodsMapper.selectGoodsByOneMenuId(res);
		PagerModel<GoodsAndUserVo> pagerModel = new PagerModel<GoodsAndUserVo>();
		Integer offset = (Integer) res.get("offset");
		Integer goodsListPageSize = (Integer) res.get("goodsListPageSize");
		Map<String, Object> resCount = new HashMap<String, Object>();
		resCount.put("oneMenuId", res.get("oneMenuId"));
		resCount.put("goodsTradeSchool", res.get("goodsTradeSchool"));
		Integer totalSize = cshtGoodsMapper.selectGoodsCountByOneMenuId(resCount);
		pagerModel.setDates(goodsAndUserVoList);
		pagerModel.setGoodsListPageSize(goodsListPageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据二级菜单查找出售中的商品
	 */
	@Override
	public PagerModel<GoodsAndUserVo> selectGoodsByTwoMenuId(Map<String, Object> res) throws Exception {
		List<GoodsAndUserVo> goodsAndUserVoList = cshtGoodsMapper.selectGoodsByTwoMenuId(res);
		PagerModel<GoodsAndUserVo> pagerModel = new PagerModel<GoodsAndUserVo>();
		Integer offset = (Integer) res.get("offset");
		Integer goodsListPageSize = (Integer) res.get("goodsListPageSize");
		Map<String, Object> resCount = new HashMap<String, Object>();
		resCount.put("twoMenuId", res.get("twoMenuId"));
		resCount.put("goodsTradeSchool", res.get("goodsTradeSchool"));
		Integer totalSize = cshtGoodsMapper.selectGoodsCountByTwoMenuId(resCount);
		pagerModel.setDates(goodsAndUserVoList);
		pagerModel.setGoodsListPageSize(goodsListPageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据三级菜单查找出售中的商品
	 */
	@Override
	public PagerModel<GoodsAndUserVo> selectGoodsByThreeMenuId(Map<String, Object> res) throws Exception {
		List<GoodsAndUserVo> goodsAndUserVoList = cshtGoodsMapper.selectGoodsByThreeMenuId(res);
		PagerModel<GoodsAndUserVo> pagerModel = new PagerModel<GoodsAndUserVo>();
		Integer offset = (Integer) res.get("offset");
		Integer goodsListPageSize = (Integer) res.get("goodsListPageSize");
		Map<String, Object> resCount = new HashMap<String, Object>();
		resCount.put("threeMenuId", res.get("threeMenuId"));
		resCount.put("goodsTradeSchool", res.get("goodsTradeSchool"));
		Integer totalSize = cshtGoodsMapper.selectGoodsCountByThreeMenuId(resCount);
		pagerModel.setDates(goodsAndUserVoList);
		pagerModel.setGoodsListPageSize(goodsListPageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}
	
	/**
	 * 根据品牌id查找出售中的商品
	 */
	@Override
	public PagerModel<GoodsAndUserVo> selectGoodsByBrandId(Map<String, Object> res) throws Exception {
		List<GoodsAndUserVo> goodsAndUserVoList = cshtGoodsMapper.selectGoodsByBrandId(res);
		PagerModel<GoodsAndUserVo> pagerModel = new PagerModel<GoodsAndUserVo>();
		Integer offset = (Integer) res.get("offset");
		Integer goodsListPageSize = (Integer) res.get("goodsListPageSize");
		Map<String, Object> resCount = new HashMap<String, Object>();
		resCount.put("brandId", res.get("brandId"));
		resCount.put("goodsTradeSchool", res.get("goodsTradeSchool"));
		Integer totalSize = cshtGoodsMapper.selectGoodsCountByBrandId(resCount);
		pagerModel.setDates(goodsAndUserVoList);
		pagerModel.setGoodsListPageSize(goodsListPageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据关键词搜索商品(一、二、三级菜单，商品标题、品牌)
	 */
	@Override
	public PagerModel<GoodsAndUserVo> selectGoodsBysousuo(Map<String, Object> res) throws Exception {
		List<GoodsAndUserVo> goodsAndUserVoList = cshtGoodsMapper.selectGoodsBysousuo(res);
		PagerModel<GoodsAndUserVo> pagerModel = new PagerModel<GoodsAndUserVo>();
		Integer offset = (Integer) res.get("offset");
		Integer goodsListPageSize = (Integer) res.get("goodsListPageSize");
		Map<String, Object> resCount = new HashMap<String, Object>();
		resCount.put("goodsSouSuoVal", res.get("goodsSouSuoVal"));
		resCount.put("goodsTradeSchool", res.get("goodsTradeSchool"));
		Integer totalSize = cshtGoodsMapper.selectGoodsBysousuoCount(resCount);
		pagerModel.setDates(goodsAndUserVoList);
		pagerModel.setGoodsListPageSize(goodsListPageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}
	
	/**
	 * 根据一级菜单id查找出售中的商品的总数量
	 */
	@Override
	public Integer selectGoodsCountByOneMenuId(Map<String, Object> res) throws Exception {
		Integer count = cshtGoodsMapper.selectGoodsCountByOneMenuId(res);
		return count;
	}

	/**
	 * 根据二级菜单id查找出售中的商品的总数量
	 */
	@Override
	public Integer selectGoodsCountByTwoMenuId(Map<String, Object> res) throws Exception {
		Integer count = cshtGoodsMapper.selectGoodsCountByTwoMenuId(res);
		return count;
	}

	/**
	 * 根据三级菜单id查找出售中的商品的总数量
	 */
	@Override
	public Integer selectGoodsCountByThreeMenuId(Map<String, Object> res) throws Exception {
		Integer count = cshtGoodsMapper.selectGoodsCountByThreeMenuId(res);
		return count;
	}

	/**
	 * 根据品牌id查找出售中的商品的总数量
	 */
	@Override
	public Integer selectGoodsCountByBrandId(Map<String, Object> res) throws Exception {
		Integer count = cshtGoodsMapper.selectGoodsCountByBrandId(res);
		return count;
	}

	/**
	 * 根据关键词搜索商品(一、二、三级菜单，商品标题、品牌)的总数量
	 */
	@Override
	public Integer selectGoodsBysousuoCount(Map<String, Object> res) throws Exception {
		Integer count = cshtGoodsMapper.selectGoodsBysousuoCount(res);
		return count;
	}

	/**
	 * 根据商品id更新goods_state为"交易中"
	 */
	@Override
	public void updateGoodsStateTradingById(Integer goodsId) throws Exception {
		cshtGoodsMapper.updateGoodsStateTradingById(goodsId);
	}

	/**
	 * 根据用户id查找出售中的商品
	 */
	@Override
	public PagerModel<GoodsAndUserVo> selectGoodsListByUserId(Map<String, Object> res) throws Exception {
		List<GoodsAndUserVo> goodsAndUserVoList = cshtGoodsMapper.selectGoodsListByUserId(res);
		PagerModel<GoodsAndUserVo> pagerModel = new PagerModel<GoodsAndUserVo>();
		Integer offset = (Integer) res.get("offset");
		Integer goodsListPageSize = (Integer) res.get("goodsListPageSize");
		Integer totalSize = cshtGoodsMapper.selectGoodsListByUserIdCount((Integer)res.get("userId"));
		pagerModel.setDates(goodsAndUserVoList);
		pagerModel.setGoodsListPageSize(goodsListPageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据用户id查找出售中的商品总数量
	 */
	@Override
	public Integer selectGoodsListByUserIdCount(Integer userId) throws Exception {
		Integer count = cshtGoodsMapper.selectGoodsListByUserIdCount(userId);
		return count;
	}


}
